package light.head.controller.el;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import light.head.constant.Msg;
import light.head.constant.Parm;
import light.head.constant.Url;
import light.head.dao.el.ClassRoomDAO;
import light.head.dao.el.ClassRoomItemDAO;
import light.head.dao.el.ClassesDAO;
import light.head.dao.sys.OrganDAO;
import light.head.enm.RESULT;
import light.head.framework.base.SupporterController;
import light.head.framework.entity.PageBean;
import light.head.model.el.ClassRoom;
import light.head.model.el.ClassRoomItem;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.google.gson.Gson;

@Controller
@RequestMapping(Url.C_CLASSROOM)
public class ClassRoomController extends SupporterController {
	/**
	 * 教室管理
	 */
	private ClassRoomDAO classRoomDAO;
	private ClassesDAO classesDAO;
	private ClassRoomItemDAO classRoomItemDAO;
	private OrganDAO organDAO;

	@RequestMapping(Url.INIT)
	public ModelAndView init(HttpServletRequest request, HttpServletResponse response){
		return new ModelAndView(Url.CLASSROOM_INIT);
	}
	
	@RequestMapping(Url.QUERY)
	public ModelAndView query(HttpServletRequest request, HttpServletResponse response){
		PageBean pb = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, Parm.NAME, Parm.DELETED);
		Integer oc = organDAO.getRoot(cu_organCode(request)).getCode();
		classRoomDAO.findByMap(organDAO.getSonCodeList(oc), map, pb);
		request.setAttribute(Parm.PAGE_BEAN, pb);
		return new ModelAndView(Url.CLASSROOM_LIST);
	}
	
	@RequestMapping(Url.ADD_EDIT)
	public ModelAndView addEdit(HttpServletRequest request, HttpServletResponse response){
		return new ModelAndView(Url.CLASSROOM_ADDEDIT);
	}
	
	@RequestMapping(Url.EDIT)
	public ModelAndView edit(HttpServletRequest request, HttpServletResponse response){
		String code = request.getParameter(Parm.CODE);
		if(isNotNull(code)){
			ClassRoom cr = classRoomDAO.get(Integer.parseInt(code));
			request.setAttribute("cr", cr);
		}
		return new ModelAndView(Url.CLASSROOM_EDIT);
	}
	
	@RequestMapping(Url.SAVE)
	public void save(ClassRoom cr,HttpServletRequest request, HttpServletResponse response){//这里只执行添加
		RESULT result = RESULT.ERROR;
		Date ct = new Date();
		cr.setCu(cu_code(request));
		cr.setOc(cu_organCode(request));
		cr.setCt(ct);
		cr.setDeleted(false);
		result = classRoomDAO.add(cr);
		
		if(RESULT.SUCCESS == result){
			print(response, Msg.SUCCESS);
		}else{
			print(response, Msg.ERROR);
		}
	}
	
	//屏蔽
	@RequestMapping(Url.SHIELD)
	public void shield(HttpServletRequest request, HttpServletResponse response){
		String code = request.getParameter(Parm.CODE);
		RESULT result = RESULT.ERROR;
		if(isNotNull(code)){
			ClassRoom cr = classRoomDAO.get(Integer.parseInt(code));
			if(cr != null){
				cr.setDeleted(true);
				result = classRoomDAO.upd(cr);
			}
		}
		
		if(result == RESULT.SUCCESS){
			print(response, Msg.SUCCESS);
		}else{
			print(response, Msg.ERROR);
		}
	}
	
	//解除屏蔽
	@RequestMapping(Url.UN_SHIELD)
	public void unshield(HttpServletRequest request, HttpServletResponse response){
		String code = request.getParameter(Parm.CODE);
		RESULT result = RESULT.ERROR;
		if(isNotNull(code)){
			ClassRoom cr = classRoomDAO.get(Integer.parseInt(code));
			if(cr != null){
				cr.setDeleted(false);
				result = classRoomDAO.upd(cr);
			}
		}
		
		if(result == RESULT.SUCCESS){
			print(response, Msg.SUCCESS);
		}else{
			print(response, Msg.ERROR);
		}
	}
	
	
	@RequestMapping(Url.AID_INIT)
	public ModelAndView aidInit(HttpServletRequest request, HttpServletResponse response){
		String code = request.getParameter(Parm.CODE);
		if(isNotNull(code)){
			ClassRoomItem item = classRoomItemDAO.get(Integer.parseInt(code));
			request.setAttribute(Parm.ID, item.getCode());
			request.setAttribute(Parm.TITLE, item.getTitle());
			request.setAttribute("crc", item.getCrc());
			request.setAttribute("clc", item.getClc());
			request.setAttribute(Parm.START, item.getStartTime());
			request.setAttribute(Parm.END, item.getEndTime());
			request.setAttribute(Parm.ALLDAY, item.getAllDay());
			request.setAttribute("remark", item.getRemark());
			request.setAttribute(Parm.DELETED, item.getDeleted());
		}else{
			request.setAttribute("crc", request.getParameter("crc"));
			request.setAttribute(Parm.ALLDAY, request.getParameter(Parm.ALLDAY));
			request.setAttribute(Parm.START, request.getParameter(Parm.START));
			request.setAttribute(Parm.END, request.getParameter(Parm.END));
		}
		return new ModelAndView(Url.CLASSROOM_AIDINIT);
	}
	
	@RequestMapping(Url.AID_LIST)
	public ModelAndView aidList(HttpServletRequest request, HttpServletResponse response){
		PageBean pb = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, Parm.NAME);
		map.put(Parm.DELETED, "false");
		classesDAO.findByMap2(cu_organCode(request), map, pb);
		request.setAttribute(Parm.PAGE_BEAN, pb);
		return new ModelAndView(Url.CLASSROOM_AIDLIST);
	}
	
	//保存教室使用明细
	@RequestMapping("saveItem")
	public void saveItem(ClassRoomItem item, HttpServletRequest request, HttpServletResponse response){
		String result = null;
		if(item.getCode() == null){
			item.setDeleted(false);
			if(classRoomItemDAO.add(item) == RESULT.SUCCESS){
				result = "{\"result\":\"success\" ," +
						" \"id\":\""+ item.getCode() +"\", " +
						" \"start\":\""+ item.getStartTime() +"\"," +
						" \"end\":\""+ item.getEndTime() +"\"," +
						" \"title\":\""+ item.getTitle() +"\"," +
						" \"allDay\":\""+ item.getAllDay() +"\"" +
						"}";
			}else{
				result = "{\"result\":\"error\"}";
			}
		}else{
			if(classRoomItemDAO.update(item) == RESULT.SUCCESS){
				result = "{\"result\":\"success\" ," +
						" \"id\":\""+ item.getCode() +"\", " +
						" \"start\":\""+ item.getStartTime() +"\"," +
						" \"end\":\""+ item.getEndTime() +"\"," +
						" \"title\":\""+ item.getTitle() +"\"," +
						" \"allDay\":\""+ item.getAllDay() +"\"" +
						"}";
			}else{
				result = "{\"result\":\"error\"}";
			}
		}
		
		print(response, result);
	}
	
	//显示班级使用明细在日历上
	@RequestMapping("showClassRoomItem")
	public void showClassRoomItem(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String crc = request.getParameter("crc");
		/*Integer oc = organDAO.getRoot(cu_organCode(request)).getCode();*/
		if(isNotNull(crc)){
			//List<ClassRoomItem> list = classRoomItemDAO.findByOc(organDAO.getSonCodeList(oc));
			List<ClassRoomItem> list = classRoomItemDAO.findByCrc(Integer.parseInt(crc));
			Gson gson = new Gson();
			String json = gson.toJson(list);
			response.setContentType("application/json;charset=utf-8");
			response.getWriter().write(json);
		}
	}
	
	//拖拽日历上的控件时修改班级使用明细
	@RequestMapping("updClassRoomItem")
	public void updClassRoomItem(HttpServletRequest request, HttpServletResponse response){
		RESULT result = RESULT.ERROR;
		String code = request.getParameter(Parm.CODE);
		String allDay = request.getParameter(Parm.ALLDAY);
		String start = request.getParameter(Parm.START);
		String end = request.getParameter(Parm.END);
		if(isNotNull(code)){
			ClassRoomItem item = classRoomItemDAO.get(Integer.parseInt(code));
			if(item != null){
				if(isNotNull(allDay)){
					item.setAllDay(Boolean.parseBoolean(allDay));
				}
				SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				try {
					item.setStartTime(isNotNull(start) ? sf.parse(start) : item.getStartTime());
					item.setEndTime(isNotNull(end) ? sf.parse(end) : null);
				} catch (Exception e) {
					e.printStackTrace();
				}
				result = classRoomItemDAO.upd(item);
			}
		}
		
		if(RESULT.SUCCESS == result){
			print(response, Msg.SUCCESS);
		}else{
			print(response, Msg.ERROR);
		}
	}
	
	@Autowired
	public void setClassRoomDAO(ClassRoomDAO classRoomDAO) {
		this.classRoomDAO = classRoomDAO;
	}

	@Autowired
	public void setClassesDAO(ClassesDAO classesDAO) {
		this.classesDAO = classesDAO;
	}

	@Autowired
	public void setClassRoomItemDAO(ClassRoomItemDAO classRoomItemDAO) {
		this.classRoomItemDAO = classRoomItemDAO;
	}
	@Autowired
	public void setOrganDAO(OrganDAO organDAO) {
		this.organDAO = organDAO;
	}
	
}
